#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pprint
from xml.dom import minidom

class ConfigNB:
	__file_name = "config.xml"

	def setFtpRunStartup(self,value): #value is a bolean
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("ftp")
			
			elements[0].childNodes[0].nodeValue = value			
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"

	def isFtpServiceRun(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("ftp")
			value = elements[0].childNodes[0].nodeValue
			
			if value == "0":
				return False
			elif value == "1":
				return True
		except:
			print "Imposible acceder a la fuente"
			return False
		
	def setFtpServerConfig(self,user,password,path): #setear las opciones para el inicio del servidor FTP
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("ftp")
			elements[0].setAttribute("user",user)
			elements[0].setAttribute("password",password)
			elements[0].setAttribute("path",path)
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"

	def getFtpServerConfig(self): #devolver un array con la informacion
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("ftp")
			data = {"user":elements[0].getAttribute("user"), "password":elements[0].getAttribute("password"), "path":elements[0].getAttribute("path")}
			return data
		except:
			print "Imposible acceder a la fuente"
			return "No data"
		
	def setPcBloqueadoConfig(self, value): #setear las opciones para el inicio del computador bloqueado al iniciar el SO
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("bloqueoPC")
			
			elements[0].childNodes[0].nodeValue = value
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"
	
	def isPcBloqueadoConfig(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("bloqueoPC")
			value = elements[0].childNodes[0].nodeValue
			
			if value == "0":
				return False
			elif value == "1":
				return True
		except:
			print "Imposible acceder a la fuente"
			return False
	
	def setWebServiceConfig(self,url,passwdDevice,perdido):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("webServer")
			
			elements[0].setAttribute("url",url)
			elements[0].setAttribute("passwdDevice",passwdDevice)
			elements[0].setAttribute("perdido",perdido)
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"
	
	def setDevicePerdido(self,perdido):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("webServer")
			elements[0].setAttribute("perdido",perdido)
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"
	
	def getWebServiceConfig(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("webServer")
			data = {"url":elements[0].getAttribute("url"), "passwordDevice":elements[0].getAttribute("passwordDevice")}
			return data
		except:
			print "Imposible acceder a la fuente"
			return "No data"
	
	def setTimeSendData(self,time):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("tiempoEnvio")
			
			elements[0].childNodes[0].nodeValue = time
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"
		
	def getTimeSendData(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("tiempoEnvio")
			return elements[0].childNodes[0].nodeValue
		except:
			print "Imposible acceder a la fuente"
			return "No data"
	
	def isDevicePerdido(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("webServer")
			value = elements[0].getAttribute("perdido")
			
			if value == "0":
				return False
			elif value == "1":
				return True
		except:
			print "Imposible acceder a la fuente"
			return False
	
	def isAlertActive(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("alerta")
			value = elements[0].childNodes[0].nodeValue
			
			if value == "0":
				return False
			elif value == "1":
				return True
		except:
			print "Imposible acceder a la fuente"
			return False
	
	def getMsgAlert(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("alerta")
			return elements[0].getAttribute("msg")
		except:
			print "Imposible acceder a la fuente"
			return "No data"
	
	def setAlertActive(self,value):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("alerta")
			
			elements[0].childNodes[0].nodeValue = value
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"
	
	def setMsgAlert(self,value):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("alerta")
			
			elements[0].setAttribute("msg",value)
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"
		
	def isKillProc(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("matarProceso")
			value = elements[0].childNodes[0].nodeValue
			
			if value == "0":
				return False
			elif value == "1":
				return True
		except:
			print "Imposible acceder a la fuente"
			return False
		
	def getProcessNameKill(self):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("matarProceso")
			return elements[0].getAttribute("proc")
		except:
			print "Imposible acceder a la fuente"
			return "No data"
	
	def setKillProcessActive(self,value):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("matarProceso")
			
			elements[0].childNodes[0].nodeValue = value
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"
		
	def setProcessNameKill(self,value):
		try:
			doc = minidom.parse(self.__file_name)
				
			elements = doc.getElementsByTagName("matarProceso")
			
			elements[0].setAttribute("proc",value)
			
			f = open(self.__file_name, "w")
			doc.writexml(f)
		except:
			print "Imposible acceder a la fuente"
			
	